﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Control.Bus;


namespace Control.Dal
{
    class DalWareHouse
    {
        internal static Control.Bus.BusWareHouse GetWHByDateAndProd(DateTime dateTime, Control.Bus.BusProduct Prod)
        {
            var whs = from wh in DalCommon.Database.WareHouses
                      where wh.IDProd == Prod.ID 

                      select new BusWareHouse()
                      {
                          ID = wh.ID,
                          Import = wh.ImPort,
                          Export = wh.ExPort,
                          Store = wh.Store,
                          Date = (DateTime)wh.Date,
                          Prod = BusProduct.GetProdByCode(wh.Product.ProdCode),
                      };
            return whs.FirstOrDefault<BusWareHouse>();
        }

        internal static void UpDate(BusWareHouse busWareHouse)
        {
            int saiso = 0;
            WareHouseDataContext db = DalCommon.Database;
            WareHouse wh = db.WareHouses.Single(w => w.ID == busWareHouse.ID);
            saiso = busWareHouse.Store - wh.Store;

            wh.ImPort = busWareHouse.Import;
            wh.ExPort = busWareHouse.Export;
            wh.Store = busWareHouse.Store;

            List<WareHouse> whs = DalCommon.Database.WareHouses.ToList<WareHouse>();
            foreach (WareHouse item in whs)
            {
                if (item.Date.ToShortDateString().CompareTo(busWareHouse.Date.ToShortDateString()) > 0 && item.Product.ID == busWareHouse.Prod.ID)
                    item.Store += saiso;
            }
            db.SubmitChanges();
        }

        internal static List<BusWareHouse> GetTonKhoByProduct(BusProduct busProduct)
        {
            var whs = from wh in DalCommon.Database.WareHouses
                      where wh.IDProd == busProduct.ID
                      orderby wh.Date descending
                      select new BusWareHouse()
                      {
                          ID = wh.ID,
                          Import = wh.ImPort,
                          Export = wh.ExPort,
                          Store = wh.Store,
                          Date = (DateTime)wh.Date,
                          Prod = BusProduct.GetProdByCode(wh.Product.ProdCode),
                      };
            return whs.ToList<BusWareHouse>();
        }

        internal static void Insert(BusWareHouse busWareHouse)
        {
            WareHouse wh = new WareHouse()
            {
                Date = busWareHouse.Date,
                ImPort = busWareHouse.Import,
                ExPort = busWareHouse.Export,
                Store = busWareHouse.Store,
                IDProd = busWareHouse.Prod.ID
            };
            DalCommon.Database.WareHouses.InsertOnSubmit(wh);
            List<WareHouse> whs = DalCommon.Database.WareHouses.ToList<WareHouse>();
            foreach (WareHouse item in whs)
            {
                if(item.Date.ToShortDateString().CompareTo(busWareHouse.Date.ToShortDateString())>0&&item.Product.ID==busWareHouse.Prod.ID)
                    item.Store += (wh.ImPort + wh.ExPort);
            }
            DalCommon.Database.SubmitChanges();
        }



        internal static List<BusWareHouse> GetAll()
        {
            var whs = from wh in DalCommon.Database.WareHouses
                      orderby wh.Date descending
                      select new BusWareHouse()
                      {
                          ID = wh.ID,
                          Import = wh.ImPort,
                          Export = wh.ExPort,
                          Store = wh.Store,
                          Date = (DateTime)wh.Date,
                          Prod = BusProduct.GetProdByCode(wh.Product.ProdCode),
                      };
            return whs.ToList<BusWareHouse>();
        }

        internal static List<BusWareHouse> GetByRangeDate(DateTime fromDate, DateTime toDate)
        {
            var whs = from wh in DalCommon.Database.WareHouses
                      where wh.Date.CompareTo(fromDate)>=0 && wh.Date.CompareTo(toDate)<=0
                      orderby wh.Date descending
                      select new BusWareHouse()
                      {
                          ID = wh.ID,
                          Import = wh.ImPort,
                          Export = wh.ExPort,
                          Store = wh.Store,
                          Date = (DateTime)wh.Date,
                          Prod = BusProduct.GetProdByCode(wh.Product.ProdCode),
                      };
            return whs.ToList<BusWareHouse>();
        }

        internal static List<BusWareHouse> GetTonKhoByProduct(BusProduct busProduct, DateTime newdate)
        {
            var whs = from wh in DalCommon.Database.WareHouses
                      where wh.IDProd == busProduct.ID && wh.Date.CompareTo(newdate)<0
                      orderby wh.Date descending
                      select new BusWareHouse()
                      {
                          ID = wh.ID,
                          Import = wh.ImPort,
                          Export = wh.ExPort,
                          Store = wh.Store,
                          Date = (DateTime)wh.Date,
                          Prod = BusProduct.GetProdByCode(wh.Product.ProdCode),
                      };
            return whs.ToList<BusWareHouse>();
        }
    }
}
